package vue;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class PopupSuggestions extends JPopupMenu implements ActionListener
{
	private List<String> _mots;
	private ControleurEditeur _controleur;
	private int _position;
	
	public PopupSuggestions(ControleurEditeur ctrl)
	{
		super();
		_controleur = ctrl;
	}
	
	public void setSuggestions(List<String> mots, int pos)
	{
		_mots = mots;
		_position = pos;
	}
	
	public void updateSuggestions()
	{
		removeAll();
		build();
	}
	
	private void build()
	{
		for (String s : _mots)
		{
			JMenuItem jmi = new JMenuItem(s);
			jmi.setActionCommand(s);
			jmi.addActionListener(this);
			add(jmi);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		_controleur.remplacerMot(e.getActionCommand(), _position);
	}
	
}
